<cfparam name="attributes.projectStatus" default="6">
<cfparam name="attributes.ORDERBY" default="projectID DESC">
<cfparam name="attributes.time1" default="startdate">
<cfparam name="attributes.time2" default="finishdate">
<cfparam name="url.adminsrch" default = "">
<cfparam name="attributes.prtlist" default="">

<cfif (isdefined("url.mprtID") and url.mprtID neq "") OR (isdefined("url.property") and url.property neq "" and url.property neq 0)>
	<cfset mprt = 0 />
	<cfif (isdefined("url.mprtID") and url.mprtID neq "")>
		<cfset mprt = url.mprtID />
	</cfif>
	<cfif (isdefined("url.property") and url.property neq "" and url.property neq 0)>
		<cfset mprt = url.property />
	</cfif>
	<cfquery name="rsprts" datasource="#Request.DSN#">
		SELECT m1.* 
		FROM myprts as m1
		WHERE m1.custid = <cfqueryparam value="#SESSION.MM_custID#" cfsqltype="cf_sql_integer">
		<cfif mprt neq 0>
		AND m1.mprtid = <cfqueryparam value="#mprt#" cfsqltype="cf_sql_integer">
		</cfif>
		AND deleted=0 
        AND duplicateRowFlag = 0
        AND hideRowFlag = 0
	</cfquery>
	<cfset attributes.prtlist = Valuelist(rsprts.prtID) />
<cfelse>
	<cfquery name="rsprts" datasource="#Request.DSN#">
		SELECT m1.* 
		FROM myprts as m1
		WHERE m1.custid = <cfqueryparam value="#SESSION.MM_custID#" cfsqltype="cf_sql_integer">
		AND deleted=0 
        AND duplicateRowFlag = 0
        AND hideRowFlag = 0
	</cfquery>
	<cfset attributes.prtlist = Valuelist(rsprts.prtID) />
</cfif>

<cfquery name="rspjs" datasource="#Request.DSN#">
SELECT Distinct p1.*  
	FROM projects as p1
    left join myprts as m1 on p1.mprtid = m1.mprtid
    left join prts as pt on m1.prtid = pt.prtid
    WHERE 1=1
	AND p1.projectStatus=#attributes.projectStatus#
	
<!---	<cfif Not(isdefined("attributes.prtlist")) OR (isdefined("attributes.prtlist") and attributes.prtlist eq "")>
		AND p1.custID = <cfqueryparam value="#SESSION.MM_custID#" cfsqltype="cf_sql_integer">
	</cfif> --->
    
	<cfif isdefined("attributes.prtlist") and attributes.prtlist neq "">
		AND pt.prtid in (<cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.prtlist#" list="yes">)
	</cfif>
	
    <cfif isNumericDate(url.startdate)>
    AND p1.#attributes.time1# >= <cfqueryparam value="#url.startdate#" cfsqltype="cf_sql_clob">
    </cfif>
    <cfif isNumericDate(url.finishdate)>
    AND p1.#attributes.time2# <= <cfqueryparam value="#url.finishdate#" cfsqltype="cf_sql_clob">
    </cfif>
    <cfif isnumeric(url.projectID) and (isdefined("url.adminsrch") and url.adminsrch eq 2)>
    AND projectID = <cfqueryparam value="#url.projectID#" cfsqltype="cf_sql_clob">
    </cfif>
     <cfif (isdefined("url.buildingnumber") and url.buildingnumber neq "") and (isdefined("url.adminsrch") and url.adminsrch eq 1)>
    AND buildingnumber= <cfqueryparam value="#url.buildingnumber#" cfsqltype="cf_sql_char">
    </cfif>
    <cfif isdefined("url.unumber") and url.unumber neq "" and (isdefined("url.adminsrch") and url.adminsrch eq 1)>
    AND unumber = <cfqueryparam value="#url.unumber#" cfsqltype="cf_sql_char">
    </cfif>
    
    <!--- <cfif isdefined("url.mprtID") and url.mprtID neq "">
    AND p1.mprtID = <cfqueryparam value="#url.mprtID#" cfsqltype="cf_sql_integer">
    </cfif> 
	<cfif isdefined("url.property") and url.property neq ""> Some search use url.property instead of url.mprtID parameter. 
    AND p1.mprtID = <cfqueryparam value="#url.property#" cfsqltype="cf_sql_integer">
    </cfif> --->
    
	<cfif isdefined("url.prtID") and url.prtID neq "">
    AND m1.prtID = <cfqueryparam value="#url.prtID#" cfsqltype="cf_sql_integer">
    </cfif> 
    <cfif isdefined("url.paid") and URL.paid neq "" and (isdefined("url.adminsrch") and url.adminsrch eq 7)>
		and (<cfif url.paid eq 0>
                paid =0 or paid is null
            <cfelse>
                paid= <cfqueryparam value="#url.paid#" cfsqltype="cf_sql_clob">
            </cfif>
            )
     </cfif>
	 <cfif isdefined("url.mincost") and isdefined("url.maxcost") and isnumeric(url.mincost) and isnumeric(url.maxcost) and (isdefined("url.adminsrch") and url.adminsrch eq 6)>
	and CTTT between <cfqueryparam value="#url.mincost#" cfsqltype="cf_sql_clob"> and <cfqueryparam value="#url.maxcost#" cfsqltype="cf_sql_clob">
    </cfif>
	<cfif isdefined("url.porder") and isdefined("url.porder") and (isdefined("url.adminsrch") and url.adminsrch eq 8)>
	and ponumber =   <cfqueryparam value="#url.porder#" cfsqltype="cf_sql_clob"> 
    </cfif>
	<cfif isdefined("url.inv") and url.inv neq "">
	and projectid =   <cfqueryparam value="#url.inv#" cfsqltype="cf_sql_clob"> 
    </cfif>
ORDER BY #attributes.ORDERBY#
</cfquery>
<cfset "#attributes.qryName#"=rspjs>
<!---<cfdump var="#rspjs#">--->
